package com.example.f8399.testdemo.activity;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.example.f8399.testdemo.R;
import com.example.f8399.testdemo.adapter.BaseBindingAdapter;
import com.example.f8399.testdemo.adapter.BindingViewHolder;
import com.example.f8399.testdemo.databinding.ActivityTestBaseAdapterBinding;
import com.example.f8399.testdemo.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 测试新写的databinding 基类
 * */
public class TestBaseAdapterActivity extends AppCompatActivity {

    ActivityTestBaseAdapterBinding mBinding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       mBinding = DataBindingUtil. setContentView(this,R.layout.activity_test_base_adapter);

        LinearLayoutManager layout = new LinearLayoutManager(this);
        mBinding.recyclerView.setLayoutManager(layout);
        DividerItemDecoration decor = new DividerItemDecoration(this, layout.getOrientation());
        mBinding.recyclerView.addItemDecoration(decor);
        final TestAdapter adapter = new TestAdapter(this);
        mBinding.recyclerView.setAdapter(adapter);

        final List<String> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add("sljl--》" + i);
        }
        adapter.setData(list);

        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                String remove = list.remove(position);
                for (String s : list) {
                    if (s.equals(remove)) {
                        list.remove(s);
                    }
                }
                adapter.setData(list);
            }
        });

    }

    class TestAdapter extends BaseBindingAdapter<String> {
        public TestAdapter(Context context) {
            super(context);
        }

        @Override
        protected int getLayoutId(int viewType) {
            return R.layout.item_group;
        }

        @Override
        protected void onBindItem(@NonNull BindingViewHolder holder, final int position) {
            ViewDataBinding binding = holder.getBinding();
            View root = binding.getRoot();
            TextView listTitle = root.findViewById(R.id.listTitle);
            listTitle.setText(getData().get(position));
            root.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(position);
                    }
                }
            });
        }
    }

}
